package com.zhaopin.theswordreferstooffer;

/**
 * @author yin.peide
 * @date 2021-06-01 14:04
 * 面试题3 二维数组中的查找
 */
public class Offer3 {

    public static void main(String[] args) {
        int[][] attr = new int[4][4];

        int[] attr1 = {1, 2, 8, 9};
        int[] attr2 = {2, 4, 9, 12};
        int[] attr3 = {4, 7, 10, 13};
        int[] attr4 = {6, 8, 11, 15};
        attr[0] = attr1;
        attr[1] = attr2;
        attr[2] = attr3;
        attr[3] = attr4;

        int target = 9;

        System.out.println(find(target, attr));
    }

    /**
     * 从左下角 进行搜索数据
     * @param target
     * @param array
     * @return
     */
    public static boolean find(int target, int[][] array) {

        int m = array.length;
        int n = array[0].length;

        for (int i = m - 1, j = 0; i >= 0 & j < n; ) {
            if (target == array[i][j]) {
                return true;
            }
            if (target < array[i][j]) {
                i--;
                continue;
            }
            if (target > array[i][j]) {
                j++;
                continue;
            }
        }
        return false;

    }


}
